<?php

/**
 * 按照趣炫要求签名方法
 * 趣炫家长监护一键下线接口
 * @author yuxun
 *
 */
class TutelageController extends Controller {
	public function __construct($id,$module=null){
		parent::__construct();
	}
	
	public function beforeAction($action){
		$local_sign = $this->getSign();

		if($local_sign != $_POST['sign']){
			$json_arr = array('state'=>0,'msg'=>'check_sign_err' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
			
// 		if(time() - $_POST['time'] > 600){
// 			$json_arr = array('state'=>0,'msg'=>'timeout' );
// 			exit(json_encode($json_arr));
// 		}
			
		return parent::beforeAction($action);
	}
	
	
	/**
	 * md5(key1=value1&key2=value2&key3=value3+api_key) ; 
其中“+”号只做连接作用实际上并没参与加密，各参数之间没有空格，按参数字段升序排列后，以键值对形式拼接.
（备注：sign签名需要md5加密而成）
	 * @return string
	 */
	protected function getSign(){
		header("Content-type:text/html;charset=utf-8");
		$key = Yii::app()->params['QX_SECRET_KEY'];
		$data = $_POST;

		unset($data['sign']);   
		ksort($data);
		$sign = md5(http_build_query($data).$key);
		
		return $sign;
	}
    
	/**
	 * 踢下线
	 * http://www.tm.com/api/?r=tutelage/kick_out
	 * http://admin.s10.q-dazzle.com:8090/api/?r=tutelage/kick_out
	 */
	public function kick_out(){
		$uid = PubRequest::R('uid');
		$ditch_id = PubRequest::R('ditch_id');
		$time = PubRequest::R('time');
		
		if($uid == ''){
			$json_arr = array('state'=>0,'msg'=>'need to parameter uid' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
		
		if($ditch_id == ''){
			$json_arr = array('state'=>0,'msg'=>'need to parameter ditch_id' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
		
		if($time == ''){
			$json_arr = array('state'=>0,'msg'=>'need to parameter time' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
		
// 		if(!isset($uid) || !isset($ditch_id) || !isset($time)){
// 			$json_arr = array('state'=>0,'msg'=>'参数不完整' );
// 			exit(json_encode($json_arr));
// 		}
		
		//根据渠道获取服务器
		$modServerChannel = new ModServerChannel();
		$list = $modServerChannel->selectServerForChannelTab($ditch_id,'mango');
		$server_array = array();
		foreach ($list as $key=>$val){
			$server_array[] = $val['server_id'];
		}
		if(count($server_array) == 0){
			$json_arr = array('state'=>0,'msg'=>'该渠道下不存在服务器' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
		
		
		$params = array();
		$params['server'] = $server_array[0];
		$params['time'] = $time;
		$params['account'] = $uid;
		$result = TutelageService::send($params);
		
// 		foreach ($server_array as $key=>$val){
// 			$params = array();
// 			$params['server'] = $val;
// 			$params['time'] = $time;
// 			$params['account'] = $uid;
// 			$result = TutelageService::send($params);
// 		}

		//$result['state'] = true;
		if($result['state']){
			$json_arr = array('state'=>1,'msg'=>'success' );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}else{
			$json_arr = array('state'=>0,'msg'=>$result['msg'] );
			ApiBaseService::doLog($_POST, $json_arr);
			exit(json_encode($json_arr));
		}
	}
	
	
   

}